﻿Imports SACommon

<ToolboxBitmap(GetType(VersionNotes), "Icon_16_SA_UP_EXEC.png")>
Public Class VersionNotes
    Private Async Sub VersionNotes_Load(sender As Object, e As EventArgs) Handles Me.Load
        If MyApp.DesignMode = False Then
            Await NonDesignMode()
        Else
            IsDesignMode()
        End If
    End Sub
    Async Function NonDesignMode() As Task
        'Check for connection and stop
        If UpdateCore.Connected(True) = False Then
            rtbNotes.Text = String.Format("Please connect to the internet to display notes for {0}.", MyApp.ApplicationTitle)
        Else
            rtbNotes.Text = String.Format("Please wait ... Loading notes for {0}.", MyApp.ApplicationTitle)
            Await LoadNotes()
        End If
    End Function
    Sub IsDesignMode()
        rtbNotes.Text = "Design mode"
    End Sub
    Async Function LoadNotes() As Task
        Dim success As Boolean
        Try
            Dim tResult = Await gUpdateVersion.CheckUpdate()
            If tResult.Result <> SAEventArgs.Results.Success Then Throw New Exception(tResult.Message)
            success = True
            If success = True Then
                'First check if updates are allowed
                If gUpdateVersion.AllowUpdate = False Then
                    Throw New Exception("Updates not allowed at the moment. Please try again later.")
                End If

                tResult = Await gUpdateVersion.ReadLatestVersionNotes
                If tResult.Result <> SAEventArgs.Results.Success Then
                    rtbNotes.Text = String.Format("The following error occured:{0}{1}", vbCrLf, tResult.Message)
                Else
                    rtbNotes.Rtf = tResult.Message
                End If
            End If
        Catch ex As Exception
            rtbNotes.Text = String.Format("The following error occured:{0}{1}", vbCrLf, ex.Message)
        End Try
    End Function
End Class
